import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-07-08
 * Time: 18:37
 */
public class test8 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            String[] strs = new String[n];
            for(int i=0;i<n;i++){
                strs[i] = in.next();
            }
            int ret = -1;
            for(int i=0;i<n;i++){
                int a = 0,b = 0;
                if(strs[i].equals(str1)){
                    a = i;
                }
                if(strs[i].equals(str2)){
                    b = i;
                }
                int res = Math.abs(a-b);
                ret = Math.min(ret,res);
            }
            System.out.println(ret);
        }
    }
}
